import cv2 as cv 
import numpy as np 

#读取原始载体图像 ,第二个参数表示提取哪个通道上的像素值
img = cv.imread("/home/heron/Desktop/linux_notes/python/img/pic001.png",0)
cv.imshow("img",img)
# 读取水印图片
watermark = cv.imread("/home/heron/Desktop/linux_notes/python/img/pic001.png",2)
cv.imshow("watermark",cv.bitwise_not(watermark))
cv.waitKey()

# 进行阈值处理
w = watermark[:,:] >200
q= watermark[:,:] <=200
watermark[w] = 1
watermark[q] = 0

cv.imshow("w",cv.bitwise_not(watermark))
cv.waitKey()

# 读取原始图片的shape
r,c = img.shape

# ============嵌入过程===========================
# 生成元素值都是254的数组
t254 = np.ones((r,c),dtype = np.uint8) * 254

# 获取img 的高7位
imgH7 = cv.bitwise_and(img,t254)
print(t254.shape)
print(imgH7.shape)

# 将watermark 嵌入到imgH7
e = cv.bitwise_or(imgH7,watermark)

cv.imshow("insert",e)
cv.waitKey()

# ============提取过程=========================
# 生成元素值都是1的数组
t1 = np.ones((r,c),dtype = np.uint8)
# 从载体图像中提取水印图像
wm = cv.bitwise_and(e,t1)


#将水印图像中的值为1处理成255，以方便显示
w = wm[:,:]>0
wm[w] = 255
cv.imshow("acquire",wm)
cv.waitKey()

cv.destroyAllWindows()

